// ustomVariable.cpp: implementation of the CustomVariable class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ustomVariable.h"
#include "ModelObject.h"
#include "CustomDBVariable.h"
#include "CustomNonDBVariable.h"
#include "SimpleExpression.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
namespace PropertySystem
{
	CustomVariable::CustomVariable()
	{
		m_pVar = NULL;
		m_nPrior = 0;
		m_bIsActive = 0;
	}
	
	CustomVariable::~CustomVariable()
	{
		
	}

	void CustomVariable::Influence()
	{
		ASSERT(m_pVar);
		if(m_strInfluenceArr.GetSize() <= 0)
			return;

		for(int i = 0; i < m_strInfluenceArr.GetSize(); i++)
		{
			CString strTemp = m_strInfluenceArr.GetAt(i);
			ASSERT(!strTemp.IsEmpty());
			PropertyVariable* pVar = m_pVar->GetOwner()->GetPropertyStack()->GetProperty(strTemp);
			ASSERT(pVar);
			ASSERT(pVar->IsCustomization());

			CustomVariable* pCVar = NULL;
			if(pVar->PropertyStyle() == PropertyVariable::eDB)
			{
				pCVar = static_cast<CustomDBVariable*>(pVar);
			}
			else
			{
				pCVar = static_cast<CustomNonDBVariable*>(pVar);
			}
			pCVar->UpdateData();
			pCVar->Influence();
		}
	}

	BOOL CustomVariable::IsCanInluence(PropertyVariable* pVar)
	{
		ASSERT(pVar);
		if(m_strInfluenceArr.GetSize() <= 0)
			return FALSE;

		for(int i = 0; i < m_strInfluenceArr.GetSize(); i++)
		{
			CString strTemp = m_strInfluenceArr.GetAt(i);
			ASSERT(!strTemp.IsEmpty());
			PropertyVariable* pTempVar = m_pVar->GetOwner()->GetPropertyStack()->GetProperty(strTemp);
			ASSERT(pTempVar);
			if(pVar->GetSymbol() == pTempVar->GetSymbol())
				return TRUE;
		}

		return FALSE;
	}

	void CustomVariable::UpdateData()
	{
	}

	void CustomVariable::Influence(const CString& strInflunce)
	{
		m_strInfluenceArr.RemoveAll();

		TCHAR buf[256];
		INT nIndex = 0;
		for(int i = 0 ; i < strInflunce.GetLength(); i++)
		{
			TCHAR c = strInflunce.GetAt(i);
			if(c != _T(','))
			{
				buf[nIndex] = c;
				nIndex++;
			}
			else
			{
				buf[nIndex] = _T('\0');
				m_strInfluenceArr.Add(buf);
				nIndex = 0;
			}			
		}
		if(nIndex != 0)
		{
			buf[nIndex] = _T('\0');
			m_strInfluenceArr.Add(buf);
			nIndex = 0;
		}
	}

	void CustomVariable::CopyCustomData(CustomVariable& Dis, const CustomVariable& Source)
	{
		Dis.m_strPrompt = Source.m_strPrompt;
		Dis.m_strPromptSource = Source.m_strPromptSource;
		Dis.m_strInflunce = Source.m_strInflunce;
		Dis.m_nPrior = Source.m_nPrior;
		Dis.m_bIsActive = Source.m_bIsActive;

		for(int i = 0; i < Source.m_strInfluenceArr.GetSize(); i++)
		{
			Dis.m_strInfluenceArr.Add(Source.m_strInfluenceArr.GetAt(i));
		}
	}
};
